<?php

namespace Module;

class UtilsModule extends BaseModule {

    public function setLocaleAction() {
        $args = \Lib\Registry::get('args');
        if(!isset($args['Locale'])){
            $result = FALSE;
        }
        if ( \Lib\Registry::get('trans')->getLocale() == $args['Locale']) {
            $result = FALSE;
        } else {
            \Lib\Registry::get('trans')->setLocale($args['Locale']);
            $result = TRUE;
        }
        if(!AJAX){
            header("Location: {$_SERVER['HTTP_REFERER']}"); 
        }
        echo json_encode(
                array('result' => $result)
        );
    }
    
    public function pageNotFoundAction(){
        $title = \Lib\Registry::get('view')->__("Page not found!");
        return $this->showTemplate(__FUNCTION__, array('title' => $title));
    }
    
    public function getCategoriesAction(){
        $categories = \Lib\Model\CategoryModel::getNotEmpty();
        
        return $this->showTemplate(__FUNCTION__, array('categories' => $categories), '');
    }
    
    public function utilitsAction(){
        
        return $this->showTemplate(__FUNCTION__, array(
            'posts'=> (array)$posts,
            'bc' => array(
                \Lib\Registry::get('view')->__('Home') => '/',
                \Lib\Registry::get('view')->__('Utilits') => ''
             ),
            'title'=> \Lib\Registry::get('view')->__('Utilits')
            ));
    }
    
    public function base64Action(){
        
        if(isset($_POST['direction']) && $_POST['direction'] == 1){
            $base64 = $_POST['base64'];
            $base64Hash = base64_encode($base64);
        } elseif(isset($_POST['direction']) && $_POST['direction'] == 2){
            $base64Hash = $_POST['base64Hash'];
            $base64 = base64_decode($base64Hash);
        } else {
            $base64 = '';
            $base64Hash = '';
        }
        
        return $this->showTemplate(__FUNCTION__, array(
            'base64' => $base64,
            'base64Hash' => $base64Hash,
            'bc' => array(
                \Lib\Registry::get('view')->__('Home') => '/',
                \Lib\Registry::get('view')->__('Utilits') => '/utilits',
                \Lib\Registry::get('view')->__('Online Base64 encoder/decoder') => '/utilits/base64',
             ),
            'title'=> \Lib\Registry::get('view')->__('Online Base64 encoder/decoder')
            ));
    }
    
    public function md5Action(){
        if(isset($_POST['md5'])){
            $md5 = $_POST['md5'];
        } else {
            $md5 = NULL;
        }
        
        
        return $this->showTemplate(__FUNCTION__, array(
            'md5' => $md5,
            'bc' => array(
                \Lib\Registry::get('view')->__('Home') => '/',
                \Lib\Registry::get('view')->__('Utilits') => '/utilits',
                \Lib\Registry::get('view')->__('Online MD5 encoder') => '/utilits/md5',
             ),
            'title'=> \Lib\Registry::get('view')->__('Online MD5 encoder')
            ));
    }
    
    public function passwordGeneratorAction() {
        $pass = array();
        if(isset($_POST['set'])){
            $set = $_POST['set'];
            
            $counter = ($set['counter'] >= 4 && $set['counter'] <= 20) ? $set['counter'] : 8;
            $regexp = '';
            
            if(!isset($set['digit']) && !isset($set['lower']) && !isset($set['upper']) && !isset($set['spec'])){
                $set['digit'] = $set['lower'] = $set['upper'] = 'On';
            }
            if(isset($set['digit'])){
                $regexp .= '0123456789';
            }
            if(isset($set['lower'])){
                $regexp .= 'abcdefghijklmnopqrstuvwxyz';
            }
            if(isset($set['upper'])){
                $regexp .= 'ABCDEFGHJKLMNOPQRSTUVWXYZ';
            }
            if(isset($set['spec'])){
                $regexp .= '%*?@#$~';
            }
            $regexpLen = strlen($regexp);
            for($i = 0; $i < 5; $i++){
                $tmpPass = '';
                for($j = 0; $j < $counter; $j++){
                    $tmpPass .= $regexp[rand(0, $regexpLen)];
                }
                $pass[] = $tmpPass;
            }
        } else {
            $set['digit'] = $set['lower'] = $set['upper'] = 'On';
            $set['counter'] = 8;
        }

        return $this->showTemplate(__FUNCTION__, array(
                    'passwords' => $pass,
                    'set' => $set,
                    'bc' => array(
                        \Lib\Registry::get('view')->__('Home') => '/',
                        \Lib\Registry::get('view')->__('Utilits') => '/utilits',
                        \Lib\Registry::get('view')->__('Password generator online') => '/utilits/passwordGenerator',
                    ),
                    'title' => \Lib\Registry::get('view')->__('Password generator online')
        ));
    }
    
    public function utilsMenuAction(){
        return $this->showTemplate(__FUNCTION__, array(), '');
    }

}

?>
